package com.mb.app.controller;

import com.mb.app.domain.SysAreaCoord;
import com.mb.app.domain.TbShipFeeCfg;
import com.mb.app.service.ISysAreaCoordService;
import com.mb.app.service.ITbShipFeeCfgService;
import com.mb.common.controller.BaseController;
import com.mb.common.utils.DateUtils;
import com.mb.common.utils.PageUtils;
import com.mb.common.utils.Query;
import com.mb.common.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 运费配置(模板)  前端控制器
 * </p>
 *
 * @author wlz
 * @since 2019-04-10
 */
@Controller
@RequestMapping("/app/shipFeeCfg")
@Slf4j
public class TbShipFeeCfgController extends BaseController {



    @Autowired
    private ITbShipFeeCfgService shipFeeCfgService;

    @Autowired
    private ISysAreaCoordService sysAreaCoordService;
    
    @GetMapping("/index")
    String index(){
        return "/app/shipFeeCfg/shipFeeCfg_list";
    }

    @ResponseBody
    @GetMapping("/list")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        // 查询列表数据
        // 时间截取
        String createTime = (String) params.get("createTime");
        if (StringUtils.isNotEmpty(createTime)) {
            params.put("startTime", DateUtils.parse(createTime.substring(0,10),DateUtils.DATE_PATTERN));
            params.put("endTime",createTime.substring(13,23)+" 23:59:59");
        }
        Query query = new Query(params);
        List<TbShipFeeCfg> list = shipFeeCfgService.queryList(query);
        int total = shipFeeCfgService.count(query);
        PageUtils pageUtils = new PageUtils(list, total);
        return pageUtils;
    }

    @GetMapping("/add")
    String add(Model model){
        // 查询所有 运费配置
        Map<String,Object> map = new HashMap<String,Object>();
        List<TbShipFeeCfg> tbShipFeeCfgs = shipFeeCfgService.queryList(map);
        List<String> sysAreaCoord = new ArrayList<String>();
        if (tbShipFeeCfgs.size() > 0) {
            for (int i = 0; i < tbShipFeeCfgs.size(); i++) {
                TbShipFeeCfg tbShipFeeCfg = tbShipFeeCfgs.get(i);
                if(tbShipFeeCfg.getAreaIds() != null && tbShipFeeCfg.getAreaIds() != "") {
                    String[] areaIds = tbShipFeeCfg.getAreaIds().split(",");
                    for(int j = 0; j < areaIds.length; j++) {
                        sysAreaCoord.add(areaIds[j]);
                    }
                }
            }
        }
        // 查询省份信息
        map.put("level",1);
        map.put("list",sysAreaCoord);
        List<SysAreaCoord> sysAreaCoords = sysAreaCoordService.getSysAreaCoord(map);
        model.addAttribute("sysAreaCoords",sysAreaCoords);
        return "/app/shipFeeCfg/shipFeeCfg_add";
    }


    @ResponseBody
    @PostMapping("/save")
    public R save(TbShipFeeCfg TbshipFeeCfg, HttpServletRequest request){
        TbshipFeeCfg.setAddUserId(getUserId());
        int result = shipFeeCfgService.save(TbshipFeeCfg);
        return R.ok();
    }


    @ResponseBody
    @PostMapping("/remove")
    public R remove(@RequestParam("id") Long id, HttpServletRequest request){
        Long delUserId = getUserId();
        int result = shipFeeCfgService.remove(id,delUserId);
        return R.ok();
    }

    @GetMapping("/dtl/{shipFeeCfgId}")
    String dtl(@PathVariable("shipFeeCfgId") Long shipFeeCfgId, Model model){
        TbShipFeeCfg shipFeeCfgInfo = shipFeeCfgService.getDtl(shipFeeCfgId);
        model.addAttribute("shipFeeCfgInfo",shipFeeCfgInfo);
        return "/app/shipFeeCfg/shipFeeCfg_info";
    }


    @GetMapping("/edit/{shipFeeCfgId}")
    String edit(@PathVariable("shipFeeCfgId") Long shipFeeCfgId, Model model){
        TbShipFeeCfg shipFeeCfgInfo = shipFeeCfgService.getDtl(shipFeeCfgId);
        model.addAttribute("shipFeeCfgInfo",shipFeeCfgInfo);
        // 查询所有 运费配置
        Map<String,Object> map = new HashMap<String,Object>();
        List<TbShipFeeCfg> tbShipFeeCfgs = shipFeeCfgService.queryList(map);
        List<String> sysAreaCoord = new ArrayList<String>();
        if (tbShipFeeCfgs.size() > 0) {
            for (int i = 0; i < tbShipFeeCfgs.size(); i++) {
                TbShipFeeCfg tbShipFeeCfg = tbShipFeeCfgs.get(i);
                if(tbShipFeeCfg.getAreaIds() != null && tbShipFeeCfg.getAreaIds() != "") {
                    String[] areaIds = tbShipFeeCfg.getAreaIds().split(",");
                    for(int j = 0; j < areaIds.length; j++) {
                        sysAreaCoord.add(areaIds[j]);
                    }
                }
            }
        }
        // 查询省份信息
        map.put("level",1);
        map.put("list",sysAreaCoord);
        List<SysAreaCoord> sysAreaCoords = sysAreaCoordService.getSysAreaCoord(map);
        model.addAttribute("sysAreaCoords",sysAreaCoords);
        List<SysAreaCoord> sysAreaCoordSelect = new ArrayList<SysAreaCoord>();
        // 查询当前配置已选择的省份
        if(shipFeeCfgInfo != null && shipFeeCfgInfo.getAreaIds() != null) {
            String[] areaIds = shipFeeCfgInfo.getAreaIds().split(",");
            for (int i = 0; i < areaIds.length; i++){
                SysAreaCoord sysAreaCoord1 = sysAreaCoordService.selectById(areaIds[i]);
                sysAreaCoordSelect.add(sysAreaCoord1);
            }
        }
        model.addAttribute("sysAreaCoordSelect",sysAreaCoordSelect);
        return "/app/shipFeeCfg/shipFeeCfg_edit";
    }


    @ResponseBody
    @PostMapping("/update")
    public R update(TbShipFeeCfg tbShipFeeCfg){
        tbShipFeeCfg.setUpdateUserId(getUserId());
        int result = shipFeeCfgService.update(tbShipFeeCfg);
        return R.ok();
    }

}
